<?php

namespace App\Http\Services;

use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Hint;
use App\Http\Model\User;
use App\Http\Model\RoomFrame;
use App\Http\Model\FinanceUserWallet;
use App\Http\Model\FinanceDiamond;
use App\Http\Model\System;
use App\Http\Model\FinanceRechargeOrder;
use App\Http\Model\Union;
use App\Http\Model\Room;
use App\Http\Services\MsgServices;


class RoomFrameServices extends CommonServices
{
    protected $userModel;
    protected $userWalletModel;
    protected $financeDiamondModel;
    protected $roomFrameModel;
    protected $msgService;
    protected $finance_service;
    protected $systemModel;
    protected $financeRechargeModel;
    protected $unionModel;
    protected $roomModel;

    public function __construct()
    {
        parent::__construct();

        $this->userModel = new User();
        $this->userWalletModel = new FinanceUserWallet();
        $this->financeDiamondModel = new FinanceDiamond();
        $this->roomFrameModel = new RoomFrame();
        $this->msgService = new MsgServices();
        $this->finance_service = new FinanceServices();
        $this->systemModel = new System();
        $this->financeRechargeModel = new FinanceRechargeOrder();
        $this->unionModel = new Union();
        $this->roomModel = new Room();
    }

    /**
     * 房间边框新增
     */
    public function roomFrameAdd($frameName, $frameImg, $frameDesc){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];
        $now = time();
        do {
            if(empty($frameName) || empty($frameImg)) {
                $code = Hint::PARAM_ERROR;
                break;
            }

            //新增
            $id = $this->roomFrameModel->addRoomFrame($frameName, $frameImg, $frameDesc);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 房间边框编辑
     */
    public function roomFrameEdit($frameId, $frameName, $frameImg, $frameDesc){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];
        $now = time();
        do {
            if(empty($frameId)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            $info = $this->roomFrameModel->getRoomFrameById($frameId);
            if(empty($info)) {
                $code = Hint::PARAM_ERROR;
                break;
            }

            //更新
            $this->roomFrameModel->editRoomFrame($frameId, ['frame_name'=>$frameName, 'frame_img'=>$frameImg, 'frame_desc'=>$frameDesc]);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 房间边框用户下发记录新增
     */
    public function roomFrameUserAdd($frameId, $roomId, $timeSt, $timeEt, $adminId){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];
        $now = time();
        do {
            if(empty($frameId) || empty($roomId) || empty($timeSt) || empty($timeEt) || empty($adminId)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            $info = $this->roomFrameModel->getRoomFrameById($frameId);
            if(empty($info)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            //边框下发时间不可重合
            $flag = 0;
            $timeStNum = strtotime($timeSt);
            $timeEtNum = strtotime($timeEt);
            $roomFrameLogList = $this->roomFrameModel->getRoomFrameByRoomId($roomId);
            foreach($roomFrameLogList as $val) {
                $val['time_st'] = strtotime($val['time_st']);
                $val['time_et'] = strtotime($val['time_et']);
                if(($timeStNum<=$val['time_st'] && $val['time_st']<=$timeEtNum) 
                || ($timeStNum<=$val['time_et'] && $val['time_et']<=$timeEtNum)
                || ($val['time_st']<=$timeStNum && $timeStNum<=$val['time_et'])
                || ($val['time_st']<=$timeEtNum && $timeEtNum<=$val['time_et'])) {
                    $flag = 1;
                    break;
                }
            }
            if($flag) {
                $code = Hint::PARAM_ERROR;
                $msg = '与现有边框时间重合';
                break;
            }

            //新增
            $id = $this->roomFrameModel->addRoomFrameLog($frameId, $roomId, $timeSt, $timeEt, $adminId);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 房间边框用户下发记录编辑
     */
    public function roomFrameUserEdit($logId, $timeSt, $timeEt, $adminId){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];
        $now = time();
        do {
            if(empty($logId) || empty($adminId)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            $info = $this->roomFrameModel->getRoomFrameLogById($logId);
            if(empty($info)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            //边框下发时间不可重合
            $flag = 0;
            $timeStNum = strtotime($timeSt);
            $timeEtNum = strtotime($timeEt);
            $roomFrameLogList = $this->roomFrameModel->getRoomFrameByRoomId($info['room_id']);
            foreach($roomFrameLogList as $val) {
                if($val['id'] == $logId) {
                    continue;
                }
                $val['time_st'] = strtotime($val['time_st']);
                $val['time_et'] = strtotime($val['time_et']);
                if(($timeStNum<=$val['time_st'] && $val['time_st']<=$timeEtNum) 
                || ($timeStNum<=$val['time_et'] && $val['time_et']<=$timeEtNum)
                || ($val['time_st']<=$timeStNum && $timeStNum<=$val['time_et'])
                || ($val['time_st']<=$timeEtNum && $timeEtNum<=$val['time_et'])) {
                    $flag = 1;
                    break;
                }
            }
            if($flag) {
                $code = Hint::PARAM_ERROR;
                $msg = '与现有边框时间重合';
                break;
            }

            //更新
            $this->roomFrameModel->editRoomFrameLog($logId, $info['room_id'], ['time_st'=>$timeSt, 'time_et'=>$timeEt, 'admin_id'=>$adminId]);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 房间边框用户下发记录删除
     */
    public function roomFrameUserDel($logId, $adminId){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];
        $now = time();
        do {
            if(empty($logId) || empty($adminId)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            $info = $this->roomFrameModel->getRoomFrameLogById($logId);
            if(empty($info)) {
                $code = Hint::PARAM_ERROR;
                break;
            }

            //更新
            $this->roomFrameModel->editRoomFrameLog($logId, $info['room_id'], ['is_delete'=>1, 'admin_id'=>$adminId]);

        } while (false);

        return compact('code', 'msg', 'data');
    }

}